#include"Dlish.h"
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct node{
    int data;
    struct list_head list;
}queue;

queue *init_queue(void){
    queue *head = malloc(sizeof(queue));
    if(head != NULL)
        INIT_LIST_HEAD(&head->list);
    return head;
}

queue *new_node(int data){
    queue *new = malloc(sizeof(queue));
    if(new != NULL){
        new->data = data;
        INIT_LIST_HEAD(&new->list);
    }
    return new;
}

bool en_queue(queue *q, int data){
    if(q != NULL){
        queue *new = malloc(sizeof(queue));
        if(new != NULL){
            new->data = data;
        INIT_LIST_HEAD(&new->list);
        }
        list_add_tail(&new->list, &q->list);
        return true;
    }
    return false;
}

bool out_queue(queue *q, int *pdata){
    if(q != NULL){
        struct list_head *tmp = q->list.next;
        list_del(tmp);
        if(pdata != NULL)
            *pdata = list_entry(tmp, queue, list)->data;
        free(list_entry(tmp, queue, list));
        return true;
    }
    return false;
}

bool within(queue *q, int data){
    struct list_head *pos;
    list_for_each(pos, &q->list){
        if(list_entry(pos, queue, list)->data == data)
            return true;
    }
    return false;
}

int main(int argc, char const *argv[])
{
    int n[] = {3, 1, 2, 5, 4, 7, 1, 5, 2, 8, 7, 3, 6, 9, 2};

    queue *q = init_queue();

    int max = 0;
    int len = 0;
    for(int i = 0; i < sizeof(n)/sizeof(int); i++){
        while(within(q, n[i])){
            out_queue(q, NULL);
            len--;
        }
        en_queue(q, n[i]);
        len++;
        max = len > max ? len : max;
    }
    printf("max: %d\n", max);
    return 0;
}
